package com.finger.forest;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.finger.forest.base.CommonKey;
import com.finger.forest.utils.ToastUtils;
import com.finger.forest.utils.encode.PrefsUtil;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.NavDestination;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications, R.id.navigation_monitor, R.id.navigation_my)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);

        //NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);

        //判断是在线登录或离线登录
        //在线登录时，首页是地图界面；离线登录时，只能进入巡护界面。
        int isOnLine = PrefsUtil.getInt(MainActivity.this,CommonKey.ISONLINE);
        Log.i(TAG,"在线登录状态："+isOnLine);
        if (isOnLine == 1){
            navController.navigate(R.id.navigation_notifications);
        } else {
            navController.navigate(R.id.navigation_dashboard);
        }

        //添加监听
        NavController.OnDestinationChangedListener listener = new NavController.OnDestinationChangedListener() {
            @Override
            public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
                if (isOnLine==0 && destination.getId()!=R.id.navigation_dashboard){
                    ToastUtils.show("离线登录，不可访问！");
                    navController.navigate(R.id.navigation_dashboard);
                }
            }
        };
        navController.addOnDestinationChangedListener(listener);

    }

}
